module.exports = function ($scope, $stateParams, slist, devicetype, create, $state,$resource, what, editurl, FileUploader) {

	//机器id
	var placecode = $stateParams.placecode;
	$scope.updateFlag = false;
	$scope.what = what;

	$scope.lock = false;

	$scope.obj = {};
	$scope.obj.view = placecode;
	//机器类型
	$scope.typearr = devicetype;
	$scope.obj.type = $scope.typearr[0].code;

	//状态
	$scope.obj.state = 1;
	$scope.obj.del_flg = 0;
	$scope.obj.auth_state = 1;
	$scope.obj.many_state = 1;
	$scope.obj.group_auth_state = 1;
	$scope.obj.printer_open = 1;
	$scope.obj.verification_printing = 1;
  $scope.obj.printing_mode = 2;
  $scope.obj.sale_state = 1;
  $scope.obj.verifi_state = 1;
  $scope.obj.inquiry_mode = 1;
  $scope.obj.rotation_angle = '0';

  $scope.pay_arr = []
	$scope.payarr = [
    {
        'label': '现金',
        'value1': '1',
        'checked': false
    },
    {
        'label': '微信',
        'value1': '2',
        'checked': false
    },
    {
        'label': '支付宝',
        'value1': '3',
        'checked': false
    },
    {
        'label': '龙支付',
        'value1': '4',
        'checked': false
    }
  ];
	//模板下拉
	$scope.search = function (pageNo){
        var para = { 
            'pageNo': pageNo || $scope.currentPage,
            'pageSize':'1000'
        }
        $resource('/api/as/tc/ticketStyle/findList', {}, {}).save(para, function(res){
            if(res.errcode === 0){
                $scope.viewlist = res.data.results;
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
		});
    }
	$scope.search();

	//景区下拉
	slist({'type': 'D'}).then(function (res) {
		if (res.errcode === 0) {
			$scope.viewarr = res.data;
			$scope.obj.view = res.data[0].code;
		}
		else {
			alert(res.errmsg);
		}
	});


	function trim(str) {
		return str.replace(/(^\s+)|(\s+$)/g, "");
	}

	 // 图片
	 var uploader2 = $scope.uploader2 = new FileUploader({
        url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=ticket&selfdir=device'+Date.parse(new Date())
    });

    uploader2.filters.push({
        name: 'imageFilter',
        fn: function(item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
	});
	
	uploader2.onSuccessItem = function(fileItem, response, status, headers) {   
        $scope.obj.img = response.savename;
    };



	$scope.gogo = function () {
    for (var i = 0; i < $scope.payarr.length; i++) {
      if ($scope.payarr[i].value == true) {
        $scope.pay_arr.push($scope.payarr[i].value1)
      }
    }
    if ($scope.pay_arr) {
      $scope.obj.pay_type = $scope.pay_arr.join(',')
    }
		if ($scope.obj.type == 5 || $scope.obj.type == 6) {
			if ($scope.obj.login_code == undefined || trim($scope.obj.login_code) == '') {
				alert('请填写登录用户')
				return;
      }
      if ($scope.obj.pass_word == undefined || trim($scope.obj.pass_word) == '') {
				alert('请填写登录密码')
				return;
      }
      $scope.obj.seller_login_code = $scope.obj.login_code + ',' + $scope.obj.pass_word
		} else {
			delete $scope.obj.print_ticket_length;
			delete $scope.obj.print_ticket_width;
			delete $scope.obj.print_ticket_x_axis;
			delete $scope.obj.print_ticket_y_axis;
		}

		create.save($scope.obj, function (res) {

			if (res.errcode !== 0) {
				alert(res.errmsg);
			}
			else {
				$state.go(editurl, { 'id': res.data.uuid });
			}

		});

	};

};